using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;

namespace DrawLine
{
    public struct Stroke
    {
        public Stroke(Color pColor, float pWidth)
        {
            Color = pColor;
            Width = pWidth;
            Points = new List<Point>();
        }

        public Color Color;
        public List<Point> Points;
        public float Width;
    }

    public class Screen
    {
        private int mID;
        private List<Stroke> mStrokes;

        public Screen(int pID)
        {
            this.mID = pID;
            this.mStrokes = new List<Stroke>();
        }

        public int ID
        {
            get
            {
                return this.mID;
            }
        }

        public void AddStroke(Stroke pStroke)
        {
            if (pStroke.Points.Count > 0)
            {
                this.mStrokes.Add(pStroke);
            }
        }

        public void Clear()
        {
            this.mStrokes.Clear();
        }

        public void Draw(Graphics pGraphics, float pScale)
        {
            pGraphics.SmoothingMode = SmoothingMode.AntiAlias;
            foreach (Stroke stroke in this.mStrokes)
            {
                // get the pen
                Pen pen = Resources.GetPen(stroke.Color, stroke.Width * pScale);
                
                // draw the strokes lines in scale
                Point lastPoint = stroke.Points[0];
                foreach (Point point in stroke.Points)
                {
                    pGraphics.DrawLine(pen, lastPoint.X * pScale, lastPoint.Y * pScale, point.X * pScale, point.Y * pScale);
                    lastPoint = point;
                }
            }
        }

        public void Save()
        {
            // save image ID (~temp\ImageList.tmp\0001.JPG)
            Random rnd = new Random();
            string path = String.Format("{0}\\ImageList.tmp", Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData));
            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
            }

            // now save this screen
            using (Bitmap bitmap = new Bitmap(Variables.THUMBNAIL_WIDTH, Variables.THUMBNAIL_HEIGHT))
            {
                this.Draw(Graphics.FromImage(bitmap), ((float)Variables.THUMBNAIL_WIDTH / (float)Variables.CANVAS_WIDTH));
                bitmap.Save(String.Format("{0}\\{1:0000}.jpg", path, this.mID));
            }
        }
    }
}
